package cn.ycbjie.ycaudioplayer.ui.study.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.synjones.minnovate.frame.basesdk.image.SynImage;

import java.util.List;

import cn.ycbjie.ycaudioplayer.R;
import cn.ycbjie.ycaudioplayer.newpack.linkagescroll.NestScrollingActivity;
import cn.ycbjie.ycaudioplayer.newpack.linkagescroll.mybeans.MyFinderlistBean;

/**
 * Created by yuyu on 2018-11-08.
 */

public class MyFinderAdapter extends RecyclerView.Adapter {
private Context context;
private List<MyFinderlistBean> mDatas;
private final LayoutInflater layoutInflater;

private static final int TYPE_ITEM   = 0;
private static final int TYPE_FOOTER = 1;

//上拉加载更多
public static final int PULLUP_LOAD_MORE = 0;
//正在加载中
public static final int LOADING_MORE     = 1;
//没有加载更多 隐藏
public static final int NO_LOAD_MORE     = 2;

//上拉加载更多状态-默认为0
private int mLoadMoreStatus = 0;

public MyFinderAdapter(Context context, List<MyFinderlistBean> mDatas) {

        this.context = context;
        this.mDatas = mDatas;
        layoutInflater = LayoutInflater.from(context);

        }

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //View view = View.inflate(parent.getContext(), R.layout.item, null);

        if (viewType==TYPE_ITEM){
        View view = layoutInflater.inflate(R.layout.finder_item, parent,false);
        return new ItemViewHolder(view);
        }else if (viewType==TYPE_FOOTER){
        View footview = layoutInflater.inflate(R.layout.loade_more, parent, false);
        return new FooterViewHolder(footview);
        }

        return null;

        }

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ItemViewHolder){
        ItemViewHolder itemViewHolder= (ItemViewHolder) holder;
        itemViewHolder.tv_title.setText(mDatas.get(position).getTitle());
            SynImage.into(context, mDatas.get(position).getCover(),itemViewHolder.img);
            itemViewHolder.tv_ids.setText(mDatas.get(position).getId());
            itemViewHolder.tv_price.setText(mDatas.get(position).getPrice());
            itemViewHolder.tv_updateinfo.setText(mDatas.get(position).getPublication_date());
            itemViewHolder.tv_updatetime.setText(mDatas.get(position).get_updated_at());
            itemViewHolder.tv_poz.setText(position+"");

        }else if(holder instanceof FooterViewHolder){
        FooterViewHolder footerViewHolder= (FooterViewHolder) holder;

        switch(mLoadMoreStatus){
//        case PULLUP_LOAD_MORE:
//        footerViewHolder.mTvLoadText.setText("上拉刷新...");
//        break;
        case LOADING_MORE:
        footerViewHolder.mTvLoadText.setText("正在加载...");
        break;
        case NO_LOAD_MORE:
        footerViewHolder.mLoadLayout.setVisibility(View.GONE);
        break;

default:

        break;
        }
        }


        }

@Override
public int getItemCount() {
        //RecyclerView的count设置为数据总条数+ 1（footerView）
        return mDatas==null?0:mDatas.size()+1;
        }

@Override
public int getItemViewType(int position) {
        //最后一个item设置为footerView
        if (position+1==getItemCount()){
        return TYPE_FOOTER;
        }else{
        return TYPE_ITEM;
        }
        }

public void addHeadItem(List<MyFinderlistBean> headDatas) {
        mDatas.addAll(0,headDatas);
        notifyDataSetChanged();
        }

public void addFootItem(List<MyFinderlistBean> footerDatas) {
        mDatas.addAll(footerDatas);
        notifyDataSetChanged();
        }

/**
 * 更新加载更多状态
 * @param state
 */
public void changeMoreState(int state){
        mLoadMoreStatus=state;
        notifyDataSetChanged();
        }


public class ItemViewHolder extends RecyclerView.ViewHolder{

    TextView tv_title;
    ImageView img;
    TextView tv_ids;
    TextView tv_updateinfo;
    TextView tv_price;
    TextView tv_updatetime,tv_poz;
    public ItemViewHolder(View itemView) {
        super(itemView);

        tv_title=itemView.findViewById(R.id.tv_title);
        tv_ids = itemView.findViewById(R.id.tv_ids);
        tv_updateinfo =  itemView.findViewById(R.id.tv_updateinfo);
        tv_price =  itemView.findViewById(R.id.tv_price);
        tv_updatetime =  itemView.findViewById(R.id.tv_updatetime);
        tv_poz = itemView.findViewById(R.id.tv_poz);

        img = itemView.findViewById(R.id.img_finderlistitem);

        initClickListen(itemView);
    }

    private void initClickListen(final View itemView) {
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                adapter.setSelectItem(arg2);
//                adapter.notifyDataSetInvalidated();
                TextView poz = (TextView) itemView.findViewById(R.id.tv_poz);

                String pz = poz.getText().toString();// arg0.getSelectedItem().toString(

//                TextView id = (TextView) itemView.findViewById(R.id.tv_ids);
//
//                String id = id.getText().toString();// arg0.getSelectedItem().toString(
//                getmp3(ids);
//                Toast.makeText(test_musicplayer.this, "you clicked id = " + ids, Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.setClass(context, NestScrollingActivity.class);
                intent.putExtra("id",mDatas.get(Integer.valueOf(pz)).getId());
                intent.putExtra("picurl",mDatas.get(Integer.valueOf(pz)).getCover());
                intent.putExtra("title",mDatas.get(Integer.valueOf(pz)).getTitle());
                intent.putExtra("created",mDatas.get(Integer.valueOf(pz)).get_created_at());
                try {
                    intent.putExtra("summary", mDatas.get(Integer.valueOf(pz)).getSummary());
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
                context.startActivity(intent);

                Toast.makeText(context, "poistion " + pz, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

public class FooterViewHolder extends RecyclerView.ViewHolder{

    ProgressBar mPbLoad;
    TextView mTvLoadText;
    LinearLayout mLoadLayout;
    public FooterViewHolder(View itemView) {
        super(itemView);
        mPbLoad= itemView.findViewById(R.id.pbLoad);
        mTvLoadText = itemView.findViewById(R.id.tvLoadText);
        mLoadLayout = itemView.findViewById(R.id.loadLayout);
    }
}
}
